route.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { NextRequest, NextResponse } from 'next/server'
  2. import { shareLinkExpiredResponse } from '@/lib/share-link-route'
  3. import { svgDownloadHeaders } from '@/lib/upload-url'
  4. import { isSharedFile, resolveSharedFileToken } from '@/lib/shared-files.server'
  5. import { readFile, stat } from 'fs/promises'
  6. import path from 'path'
  7. import { uploadsRoot } from '@/lib/upload-root'
  8. const MIME_TYPES: Record<string, string> = {
  9. jpg: 'image/jpeg',
  10. jpeg: 'image/jpeg',
  11. png: 'image/png',
  12. webp: 'image/webp',
  13. avif: 'image/avif',
  14. pdf: 'application/pdf',
  15. csv: 'text/csv',
  16. txt: 'text/plain',
  17. mp4: 'video/mp4',
  18. webm: 'video/webm',
  19. mov: 'video/quicktime',
  20. }
  21. export async function GET(
  22. _request: NextRequest,
  23. { params }: { params: Promise<{ token: string; path: string[] }> }
  24. ) {
  25. const { token, path: segments } = await params
  26. // Expected: [category, filename]
  27. if (segments.length !== 2) {
  28. return NextResponse.json({ error: 'Invalid path' }, { status: 400 })
  29. }
  30. const [category, filename] = segments
  31. // Every folder a shared document's images and attachments can live in. A
  32. // tire-hotel job's photos sat under its own folder, which this list did not
  33. // know, so its share link showed every image but those.
  34. const allowedCategories = [
  35. 'vehicles',
  36. 'inventory',
  37. 'services',
  38. 'logos',
  39. 'quotes',
  40. 'tire-hotel',
  41. 'inspections',
  42. 'documents',
  43. ]
  44. if (!allowedCategories.includes(category)) {
  45. return NextResponse.json({ error: 'Invalid category' }, { status: 400 })
  46. }
  47. // Prevent directory traversal
  48. if (filename.includes('..') || filename.includes('/') || filename.includes('\\')) {
  49. return NextResponse.json({ error: 'Invalid filename' }, { status: 400 })
  50. }
  51. // One lookup of the token answers both whether its link is still open and
  52. // which document's files it opens. A document's pictures close with its
  53. // link. A file the document does not show answers exactly as a missing one
  54. // does, so the route never says whether a name exists.
  55. const shared = await resolveSharedFileToken(token)
  56. if (shared.status === 'expired') return shareLinkExpiredResponse()
  57. if (shared.status !== 'open' || !(await isSharedFile(shared.document, category, filename))) {
  58. return NextResponse.json({ error: 'Not found' }, { status: 404 })
  59. }
  60. const orgId = shared.document.organizationId
  61. const filePath = path.join(uploadsRoot(), orgId, category, filename)
  62. try {
  63. await stat(filePath)
  64. } catch {
  65. return NextResponse.json({ error: 'Not found' }, { status: 404 })
  66. }
  67. // Never cached: a cache would keep serving the file after the link closed,
  68. // and a portal customer's file must not reach anyone else.
  69. const buffer = await readFile(filePath)
  70. const ext = filename.split('.').pop()?.toLowerCase() || ''
  71. if (ext === 'svg') {
  72. return new NextResponse(buffer, {
  73. headers: svgDownloadHeaders('private, no-store'),
  74. })
  75. }
  76. const contentType = MIME_TYPES[ext] || 'application/octet-stream'
  77. return new NextResponse(buffer, {
  78. headers: {
  79. 'Content-Type': contentType,
  80. 'Cache-Control': 'private, no-store',
  81. 'X-Robots-Tag': 'noindex, nofollow',
  82. },
  83. })
  84. }